package com.zhoug.ocr.textin.http;

import androidx.lifecycle.LifecycleOwner;

import com.uber.autodispose.AutoDispose;
import com.uber.autodispose.ObservableSubscribeProxy;
import com.uber.autodispose.android.lifecycle.AndroidLifecycleScopeProvider;
import com.zhoug.common.content.Optional;
import com.zhoug.common.framework.rxjava2.SchedulerUtils;
import com.zhoug.common.http.core.RetrofitBuilder;
import com.zhoug.common.http.interceptors.HttpLogInterceptor;
import com.zhoug.ocr.textin.TextInConfig;
import com.zhoug.ocr.textin.http.core.MyHttpLogInterceptor;
import com.zhoug.ocr.textin.http.core.TextInResponse;
import com.zhoug.ocr.textin.http.core.TextInResponseTransformer;
import com.zhoug.ocr.textin.http.core.TextInTokenInterceptor;

import io.reactivex.Observable;
import retrofit2.Retrofit;

/**
 * @Author: zhoug
 * @Date: 2023/2/20
 * @Description:
 */
public class TextInHttpClient {
    private static final String TAG = ">>>HttpClient";
    private Retrofit retrofit;
    //服务器请求接口
    private TextInHttpRequests requestServer;


    private static TextInHttpClient instance;

    public static TextInHttpClient getInstance() {
        if (instance == null) {
            synchronized (TextInHttpClient.class) {
                if (null == instance) {
                    instance = new TextInHttpClient();
                }
            }
        }
        return instance;
    }


    protected TextInHttpClient() {
        init();
    }



    /**
     * 初始化
     */
    @SuppressWarnings("unchecked")
    private void init() {
        MyHttpLogInterceptor httpLogInterceptor = new MyHttpLogInterceptor();
        httpLogInterceptor.setLevel(HttpLogInterceptor.Level.BODY);
        retrofit = new RetrofitBuilder()
                .setDebug(getDebug())
                .addInterceptor(new TextInTokenInterceptor())
                .addInterceptor(httpLogInterceptor)
                .build(TextInConfig.TEXTIN_BASE_URL);

        requestServer = retrofit.create(TextInHttpRequests.class);

    }

    /**
     * 释放资源
     * 重置BaseUrl
     */
    public void release() {
        retrofit = null;
        requestServer = null;

    }


    /**
     * debug
     *
     * @return
     */
    protected boolean getDebug() {
        return false;
    }

    /**
     * 服务器请求接口
     *
     * @return
     */
    public TextInHttpRequests getRequestMethods() {
        if (requestServer == null) {
            synchronized (this) {
                if (requestServer == null) {
                    init();
                }
            }
        }
        return requestServer;
    }


    public static <T> ObservableSubscribeProxy<Optional<T>> handler(Observable<TextInResponse<T>> observable, LifecycleOwner lifecycleOwner) {
        return observable.compose(SchedulerUtils.apply())
                .compose(TextInResponseTransformer.handleResult())
                .as(AutoDispose.autoDisposable(AndroidLifecycleScopeProvider.from(lifecycleOwner)));

    }


    public static <T> Observable<Optional<T>> handler(Observable<TextInResponse<T>> observable) {
        return observable.compose(SchedulerUtils.apply())
                .compose(TextInResponseTransformer.handleResult());
    }


}
